package models

import (
	"github.com/astaxie/beego/logs"
	"github.com/astaxie/beego/orm"
	"time"
	"tools"
)

type Messages struct {
	Id         string `orm:"column(id);pk" json:"id" form:"id"`
	UserId     string `orm:"column(user_id)" json:"user_id" form:"user_id"`
	FromUserId string `orm:"column(from_user_id)" json:"from_user_id" form:"from_user_id"`
	Type       string `orm:"column(type)" json:"type" form:"type"`
	Sta        string `orm:"column(sta)" json:"sta" form:"sta"`
	Title      string `orm:"column(title)" json:"title" form:"title"`
	Content    string `orm:"column(content)" json:"content" form:"content"`
	Voice      string `orm:"column(voice)" json:"voice" form:"voice"`
	CreateTime int64  `orm:"column(create_time)" json:"create_time" form:"create_time"`
	FromUser   *User  `orm:"-" json:"from" form:"-"`
}

func (m *Messages) TableName() string {
	return "messages"
}

func init() {
	orm.RegisterModel(new(Messages))
}

func NewMessages() *Messages {
	return &Messages{}
}

// 添加日志
func AddMessage(uid, title, content string, params ...interface{}) error {
	sql := "INSERT INTO `messages` (`id`, `user_id`, `title`, `content`, `create_time`, `type`) VALUES (?, ?, ?, ?, ?, ?)"
	_, err := orm.NewOrm().Raw(sql, tools.GUUID(), uid, title, content, time.Now().Unix(), "log").Exec()
	if err != nil {
		logs.Warn("add messages error:", err)
		return err
	}
	return nil
}